use serde::Serializer;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum AppError {
    #[error("HTTP 请求错误: {0}")]
    HttpError(#[from] reqwest::Error),

    #[error("数据库 错误: {0}")]
    SqlxError(#[from] sqlx::Error),

    #[error("解析字符串为json 错误: {0}")]
    JsonError(#[from] serde_json::Error),
}

impl serde::Serialize for AppError {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.to_string().as_ref())
    }
}
